<?php
/**
 * @copyright meila.com
 * @author gaojian3@
 * @name   修改美啦宣言
 * @param 
 *         $nick = 作用
 * 
 * 
 */
include('../../__global.php');
include(SERVER_ROOT_PATH.'/include/config/dataRule/ml_datarule_user.php');
class aj_updateDeclaration extends ml_controller 
{
    private $newDeclaration;
    private $oldDeclaration;
    private $uid;
    private $oBrief;

    function initParam(){
        $this->newDeclaration=$this->input('declaration','post');

        //获取uid
        $this->uid = $this->__visitor['uid'];
        //实例化用户模型
        $this->oBrief = new ml_model_dbUserBriefInfo();
        
        if($this->oBrief->getPersonByUid($this->uid)){
            $briefData=$this->oBrief->get_data();
            $this->oldDeclaration = $briefData['declaration'];
        }else{
            $this->api_output(ML_RCODE_BUSY);
        }
        $this->newDeclaration=Tool_keyword::filterSensitiveWord($this->newDeclaration);
    }
    /**
     * @develop_template
     * 检查参数合法性
     */
    function checkParam(){
        $this->check_referer();
        if(!$this->check_permission(ML_PERMISSION_LOGIN_ONLY)) {
            $this->api_output(ML_RCODE_NOLOGIN);
        }
        else if(!$this->check_permission(ML_PERMISSION_LOGIN_CANWRITE)) {
            $this->api_output(ML_RCODE_NOACTIVE);
        }
        
        //与原宣言比对
        if($this->newDeclaration == $this->oldDeclaration){
            $this->api_output(ML_RCODE_BUSY);
        }
        
        //检查字数限制
        if(Tool_string::str_width($this->newDeclaration) > ML_DATARULE_USER_DECLARATION_MAXLEN) 
            $this->api_output(ML_RCODE_PARAM);                    
        
        
    }
    
    /**
     * @develop_template
     * 核心业务逻辑
     */
    function main()
    {
        $arrData=array();
        $rs=$this->oBrief->updateDeclaration($this->uid,$this->newDeclaration);
        
        if($rs == false){
            $this->api_output(ML_RCODE_BUSY, '' , ML_RMESSAGE_BUSY);
        }
        $oGenerator = new ml_model_dbIdGenerator();
        $userfeed_res=$oGenerator->get_userfeed_rid($this->uid,$rid);
        if(!$userfeed_res){
            $this->api_output(ML_RCODE_BUSY , '' , ML_RMESSAGE_BUSY);
        }
        
        $data=array(
            'uid' => $this->uid,
            'rid' => $rid,
            'content' => $this->newDeclaration ,
            'type' => ML_FEED_TYPE_MOD_DEC,
            'ctime' => date('Y-m-d H:i:s',time()),
            'cip' => Tool_ip::get_real_ip()
        );
        $oUserfeed=new ml_model_dbUserfeed();
        if($oUserfeed->insert_content($data)){
            
            ml_tool_sendMq::add_feed(array('uid' => $this->uid, 'feed_rid'=> $rid));
            $this->api_output(ML_RCODE_SUCC, '');
        }else{
            $this->api_output(ML_RCODE_BUSY , '' , ML_RMESSAGE_BUSY);
        }
    }
}

new aj_updateDeclaration();
